package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.dto.AccidentReportDto;
import com.jhgsys.internal.accident.dto.AccidentReportReasonDto;
import com.jhgsys.internal.accident.entity.AccidentReport;
import com.jhgsys.internal.accident.entity.AccidentReportReason;
import com.jhgsys.internal.accident.mapper.AccidentReportReasonMapper;
import com.jhgsys.internal.accident.mapper.AccidentReportRectificationMapper;
import com.jhgsys.internal.accident.service.IAccidentReportReasonService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-08-26 16:09:46
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportReasonServiceImpl extends ServiceImpl<AccidentReportReasonMapper, AccidentReportReason> implements IAccidentReportReasonService {

    @Resource
    private AccidentReportRectificationMapper accidentReportRectificationMapper;

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param request QueryRequest
     * @param accidentReportReason accidentReportReason
     * @return IPage<AccidentReportReason>
    */
    @Override
    public IPage<AccidentReportReason> findAccidentReportReasonPage(QueryRequest request, AccidentReportReason accidentReportReason) {
        LambdaQueryWrapper<AccidentReportReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportReason::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportReason> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param accidentReportReason accidentReportReason
     * @return List<AccidentReportReason>
     */
    @Override
    public List<AccidentReportReason> findAccidentReportReasonList(AccidentReportReason accidentReportReason) {
	    LambdaQueryWrapper<AccidentReportReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportReason::getDeletedFlg, 0);
        //事故报告ID accident_report->report_id
        if(StringUtils.isNotBlank(accidentReportReason.getReportId())){
            queryWrapper.eq(AccidentReportReason::getReportId, accidentReportReason.getReportId());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-08-26 16:09:46
    * @param id
    * @return AccidentReportReasonDto
    */
    @Override
    public AccidentReportReasonDto findAccidentReportReasonById(String id){
        AccidentReportReason accidentReportReason = this.baseMapper.selectById(id);
        if(null != accidentReportReason ){
            AccidentReportReasonDto accidentReportReasonDto = BeanHelper.copyProperties(accidentReportReason, AccidentReportReasonDto.class);
            return accidentReportReasonDto;
        }
        return new AccidentReportReasonDto();
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param accidentReportReason accidentReportReason
     */
    @Override
    @Transactional
    public void createAccidentReportReason(AccidentReportReason accidentReportReason) {
        accidentReportReason.setCreatedTime(new Date());
        this.save(accidentReportReason);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param accidentReportReason accidentReportReason
     */
    @Override
    @Transactional
    public void updateAccidentReportReason(AccidentReportReason accidentReportReason) {
        accidentReportReason.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentReportReason);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param accidentReportReason accidentReportReason
     * @param accidentReportReasonIds accidentReportReasonIds
     */
    @Override
    @Transactional
    public void deleteAccidentReportReason(AccidentReportReason accidentReportReason,String accidentReportReasonIds) {
        List<String> list = Arrays.asList(accidentReportReasonIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentReportReason,new LambdaQueryWrapper<AccidentReportReason>().in(AccidentReportReason::getReportReasonId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-08-26 16:09:46
    * @param accidentReportReasonIds accidentReportReasonIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentReportReason(String accidentReportReasonIds) {
        List<String> list = Arrays.asList(accidentReportReasonIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportReason> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportReason::getReportReasonId,list);
        this.remove(wapper);
    }

    /**
     * 根据事故报告id删除
     * @author admin
     * @date 2024-08-27 10:56:15
     * @param projectIds
     */
    @Override
    @Transactional
    public void deleteByReportIds(String projectIds) {
        List<String> list = Arrays.asList(projectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportReason> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentReportReason::getReportId,list);
        this.remove(wrapper);
    }

    /**
     * 根据AccidentReport查询（分页）
     * @author zhangwei
     * @date 2024-08-26 16:09:46
     * @param request QueryRequest
     * @param accidentReportDto accidentReportDto
     * @return IPage<AccidentReportReason>
     */
    @Override
    public IPage<AccidentReportReasonDto> findPageByReport(QueryRequest request, AccidentReportDto accidentReportDto){
//        Page<AccidentReportReasonDto> page = new Page<>(request.getPageNum(), request.getPageSize());
//        return accidentReportRectificationMapper.findPageByReport(page, accidentReportDto);
        return null;
    }

}
